package com.itboye.jigongbao.fragments

import android.annotation.SuppressLint
import android.content.Intent
import android.view.Gravity
import android.view.View
import android.widget.RelativeLayout
import com.itboye.jigongbao.R
import com.itboye.jigongbao.activity.AdviceActivity
import com.itboye.jigongbao.activity.MineActivity
import com.itboye.jigongbao.app.MyApplication
import com.itboye.jigongbao.base.BaseFragment
import com.itboye.jigongbao.retrofit.base.ResultEntity
import com.itboye.jigongbao.retrofit.presenter.UserPrestener
import com.itboye.jigongbao.utils.Const
import com.itboye.jigongbao.utils.Const.Companion.avatar
import com.itboye.jigongbao.utils.TitleBarUtils
import com.itboye.jigongbao.utils.ToastUtil.ByAlert
import com.itboye.jigongbao.utils.XGlideLoader
import com.itboye.jigongbao.web.WebActivity
import com.itboye.jigongbao.widget.SharePopupWindow
import com.itboye.jigongbao.widget.titlebarwidget.TranslucentActionBar
import kotlinx.android.synthetic.main.fragment_me.*
import java.util.*



/**
 * Created by Administrator on 2017/11/28.
 */
@SuppressLint("ValidFragment")
class MeFragment(private val index: Int) : BaseFragment(), Observer {

    var userPrestener: UserPrestener? = null
    override fun update(p0: Observable?, p1: Any?) {
        var entity = p1 as ResultEntity
        ByAlert.alert(entity.toString())
    }

    var trans_action_bar: TranslucentActionBar? = null
    var re_setting: RelativeLayout? = null
    var re_help_center: RelativeLayout? = null
    var re_suggestion: RelativeLayout? = null
    var re_recommend: RelativeLayout? = null
    //    var index = IndexFragment@ index
    @SuppressLint("SetTextI18n")
    override fun initData() {
        trans_action_bar = activity.findViewById(R.id.trans_action_bar_me)
        TitleBarUtils().initTitlebarStyle1(activity, this!!.trans_action_bar!!, "我的", 0, "", 0, "")
        userPrestener = UserPrestener(this)
    }

    override fun onResume() {
        super.onResume()
        if (MyApplication.instance!!.mACache!!.getAsObject(Const.LOGIN_INFO) != null) {
            setUseData()
        }
    }

    private fun setUseData() {
        txt_username!!.text = MyApplication.instance!!.loginInfo!!.getNickname()
        txt_sign!!.text = MyApplication.instance!!.loginInfo!!.getSign()
        XGlideLoader.displayImageCircularForUser(activity, avatar + MyApplication.instance!!.loginInfo!!.getUid(), img_head)
    }

    private lateinit var mPop: SharePopupWindow

    override fun onClick(p0: View?) {
        var intent: Intent? = null
        when (p0!!.id) {
            R.id.re_setting -> {
                intent = Intent(activity, MineActivity::class.java)
                startActivity(intent)
            }
            R.id.re_help_center -> {
                intent = Intent(activity, WebActivity::class.java)
                intent.putExtra("title", "帮助中心")
                intent.putExtra("url", Const.WEB_HELP_CENTER)
                startActivity(intent)
//                userPrestener!!.getConfig("android", activity)
            }

            R.id.re_suggestion -> {
                intent = Intent(activity, AdviceActivity::class.java)
                intent.putExtra("title", "意见反馈")
//                intent.putExtra("url", Const.SUGGESTION_URL)
                startActivity(intent)
            }
            R.id.re_recommend -> {
                mPop = SharePopupWindow(this)
                mPop.showAtLocation(p0, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
            }
            R.id.txt_weixin -> {
                mPop.dismiss()
            }
            else -> {
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_me
    }

}